import { defineStore } from "pinia";
import { ref, computed } from "vue";
import {
  selectRainfallRegulativePotByCode,
  selectWaterDevPotByCode,
  selectAutoMedicateDevPotByCode,
  selectSludgeTreatmentDevPotByCode,
} from "@/utils/pots";

const storeSetup = () => {
  /* 定义数据--------------------------------------------------------------------- */
  // 雨水调节池数据
  const rainfallRegulativeData = ref({
    params: [],
    valves: [],
  });

  // 高效水处理设备数据
  const waterDevData = ref({
    params: [],
    valves: [],
  });

  // 自动投药装置数据
  const autoMedicateDevData = ref({
    params: [],
    valves: [],
  });

  // 污泥处理设备数据
  const sludgeTreatmentDevData = ref({
    params: [],
    valves: [],
  });

  /* 定义函数--------------------------------------------------------------------- */
  // 更新雨水调节池数据
  const updateRainfallRegulativeData = (data) => {
    data.params.forEach((item) => {
      // 查询点位数据
      let { top, left, width, height, size } =
        selectRainfallRegulativePotByCode(item.code);
      item.top = top;
      item.left = left;
      item.width = width;
      item.height = height;
      item.size = size;
    });
    data.valves.forEach((item) => {
      // 查询点位数据
      let { top, left, fontSize, blockSize } =
        selectRainfallRegulativePotByCode(item.code);
      item.top = top;
      item.left = left;
      item.fontSize = fontSize;
      item.blockSize = blockSize;
    });
    rainfallRegulativeData.value.params = data.params;
    rainfallRegulativeData.value.valves = data.valves;

    console.log("#更新雨水调节池数据", rainfallRegulativeData.value);
  };

  // 更新高效水处理设备数据
  const updateWaterDevData = (data) => {
    data.params.forEach((item) => {
      // 查询点位数据
      let { top, left, width, height, size } = selectWaterDevPotByCode(
        item.code
      );
      item.top = top;
      item.left = left;
      item.width = width;
      item.height = height;
      item.size = size;
    });
    data.valves.forEach((item) => {
      // 查询点位数据
      let { top, left, fontSize, blockSize, type } = selectWaterDevPotByCode(
        item.code
      );
      item.top = top;
      item.left = left;
      item.fontSize = fontSize;
      item.blockSize = blockSize;
      item.type = type;
    });
    waterDevData.value.params = data.params;
    waterDevData.value.valves = data.valves;

    console.log("#更新高效水处理设备数据", waterDevData.value);
  };

  // 更新自动投药装置数据
  const updateAutoMedicateDevData = (data) => {
    data.params.forEach((item) => {
      // 查询点位数据
      let { top, left, width, height, size } = selectAutoMedicateDevPotByCode(
        item.code
      );
      item.top = top;
      item.left = left;
      item.width = width;
      item.height = height;
      item.size = size;
    });
    data.valves.forEach((item) => {
      // 查询点位数据
      let { top, left, fontSize, blockSize, type } =
        selectAutoMedicateDevPotByCode(item.code);
      item.top = top;
      item.left = left;
      item.fontSize = fontSize;
      item.blockSize = blockSize;
      item.type = type;
    });
    autoMedicateDevData.value.params = data.params;
    autoMedicateDevData.value.valves = data.valves;

    console.log("#更新自动投药装置数据", autoMedicateDevData.value);
  };

  // 更新污泥处理设备数据
  const updateSludgeTreatmentDevData = (data) => {
    data.params.forEach((item) => {
      let { top, left, width, height, size } =
        selectSludgeTreatmentDevPotByCode(item.code);
      item.top = top;
      item.left = left;
      item.width = width;
      item.height = height;
      item.size = size;
    });
    data.valves.forEach((item) => {
      // 查询点位数据
      let { top, left, fontSize, blockSize, type } =
        selectSludgeTreatmentDevPotByCode(item.code);
      item.top = top;
      item.left = left;
      item.fontSize = fontSize;
      item.blockSize = blockSize;
      item.type = type;
    });
    sludgeTreatmentDevData.value.params = data.params;
    sludgeTreatmentDevData.value.valves = data.valves;

    console.log("#更新污泥处理设备数据", sludgeTreatmentDevData.value);
  };

  // 返回参数
  return {
    rainfallRegulativeData,
    waterDevData,
    autoMedicateDevData,
    sludgeTreatmentDevData,
    updateRainfallRegulativeData,
    updateWaterDevData,
    updateAutoMedicateDevData,
    updateSludgeTreatmentDevData,
  };
};

// 导出Store
export const useWsDataStore = defineStore("wsData", storeSetup, {
  persist: true,
});
